
const Server = require('dono-server');
const path = require('path');
const axios = require('axios');


let s = new Server({
    port: '9000',
    root: path.join(__dirname),
    https: true,
    routerData: {
        // 获取房间列表
        '/getRoomList': {
            method: 'post',
            data: async function(param, store, ctx) {
                let roomList = await store.room.get('roomList');
                let {floor, unit} = param;

                if (!floor) {
                    let floorNum = roomList.map(item => `${item.floor}-${item.unit}`);
                    floorNum = [...new Set([...floorNum])]
                    return {
                        code: '200',
                        data: {
                            floorList: floorNum
                        }
                    }
                }

                let currentFloor = roomList.filter(item => {
                    return item.floor - 0 === floor - 0 && item.unit - 0 === unit - 0;
                });

                return {
                    code: '200',
                    data: {
                        roomList: currentFloor
                    }
                }
            }
        },
        // 获取用户角色
        '/getUserInfo': {
            method: 'post',
            data: async function(param, store, ctx) {
                // 管理员  工头  工人  保洁
                // manager  teamLeader  worker  cleaner
                return {
                    code: '200',
                    data: {
                        role: 'superManager'
                    }
                };
            }
        },
        '/getUserId': {
            method: 'get',
            data: async function(param, store, ctx) {
                let {code} = param;
                
                let resToken = await axios.get('https://api.weixin.qq.com/cgi-bin/token', {
                    params: {
                        grant_type: 'client_credential',
                        appid: 'wx2d23d03693a64f66',
                        secret: 'e5564dae7b6846d4dd531811c9070cf4'
                    }
                });

                let token = resToken.data.access_token;
                
                let resUser = await axios({
                    url: `https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=${token}`,
                    method: 'post',
                    data: {
                        code: code
                    }
                });

                console.log(resUser.data);
                
                return param;
            }
        }
    },
    store: [{
        basePath: path.join(__dirname, './data'),
        jsonFile: 'room.json'
    }, {
        basePath: path.join(__dirname, './data'),
        jsonFile: 'user.json'
    }, {
        basePath: path.join(__dirname, './data'),
        jsonFile: 'token.json'
    }, {
        basePath: path.join(__dirname, './data'),
        jsonFile: 'checkIn.json'
    }]
});

// let roomList = [];
// let floor = 10;

// let number = ['01', '02', '03', '04']

// while(floor > 0) {
//     let unit = 3;

//     while(unit > 0) {
//         let level = 12;
//         while(level > 0) {
//             number.forEach((num) => {
//                 roomList.push({
//                     "name": `${floor}-${unit}-${level < 10 ? `0${level}` : level}${num}`,
//                     "id": `${floor}${unit}${level < 10 ? `0${level}` : level}${num}`,
//                     "floor": floor,
//                     "unit": unit,
//                     "number": `${level < 10 ? `0${level}` : level}${num}`,
//                     "state": 1
//                 });
//             });
//             level--;
//         }
//         unit--;
//     }
//     floor--;
// };

// s.store.room.set({
//     roomList: roomList
// });